package com.example.demo3.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.DictData;
import com.example.demo3.entity.DictType;
import com.example.demo3.model.TreeNode;
import com.example.demo3.service.DictDataService;
import com.example.demo3.service.DictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dict")
public class DictController extends BaseController{

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private DictDataService dictDataService;

    @RequestMapping("/list")
    public String deviceHtml(HttpServletRequest request){
        List<TreeNode> treeList = new ArrayList<>();
        TreeNode rootTree = new TreeNode();
        rootTree.setId("0");
        rootTree.setPId("-1");
        rootTree.setIcon("/static/imgs/tree/company.png");
        rootTree.setType(0);
        rootTree.setName("全部字典");
        treeList.add(rootTree);
        List<DictType> dictTypeList = dictTypeService.getAllType();
        for (DictType dictType:dictTypeList) {
            TreeNode treeDictType = new TreeNode();
            treeDictType.setName(dictType.getDictName());
            treeDictType.setId(dictType.getId());
            treeDictType.setPId("0");
            treeDictType.setType(1);
            treeDictType.setIcon("/static/imgs/tree/cate.png");
            rootTree.setOpen(true);
            treeList.add(treeDictType);
            List<DictData> dictDataList = dictDataService.getDictDataByType(dictType.getDictType());
            for (DictData dictData:dictDataList){
                TreeNode treedictData = new TreeNode();
                treedictData.setId(dictData.getId());
                treedictData.setType(2);
                treedictData.setPId(StringUtils.isBlank(dictData.getParentId())?dictType.getId():dictData.getParentId());
                treedictData.setName(dictData.getDictText());
                treedictData.setIcon("/static/imgs/tree/material.png");
                treeList.add(treedictData);
            }
        }
        String json = JSONArray.toJSONString(treeList);
        request.setAttribute("trees", json);
        return "dict/list";
    }

    /****
     * 加载表格数据
     * @param pageindex
     * @param pagesize
     * @param sEcho
     * @return
     */
    @RequestMapping("/ajaxDatatable")
    @ResponseBody
    public DataPageMessage ajaxDatatable(int pageindex, int pagesize, int sEcho, String type, String dictText,String id){
        //根据ID查找字典类型TYPE
        if(StringUtils.isNotBlank(id)){
            DictType dictType = dictTypeService.getDciTypeById(id);
            if(null != dictType){
                type = dictType.getDictType();
                id = "";
            }

            if(id.equals("0")){
                id = "";
            }
        }
        Page<DictData> page = dictDataService.getSelectPage(type,dictText,id,pageindex,pagesize);
        if(page==null||page.getRecords()==null){
            return DataPageMessage.ok(null,(int)page.getCurrent(),(int)page.getCurrent(),sEcho,"");
        }
        return DataPageMessage.ok(page.getRecords(),(int)page.getTotal(),(int)page.getTotal(),sEcho,"");
    }

    /********************
     * 新增字典类型
     * @return
     */
    @RequestMapping("/addDictType")
    public String addDictType(){
        return "dict/addDictType";
    }

    @RequestMapping("/saveDictType")
    @ResponseBody
    public ResponseMessage saveDictType(DictType dictType){
        return dictTypeService.saveDictType(dictType);
    }

    /**************
     * 新增字典数据
     * @return
     */
    @RequestMapping("/addDictData")
    public String addDictData(HttpServletRequest request){
        List<TreeNode> treeList = new ArrayList<>();
        List<DictType> dictTypeList = dictTypeService.getAllType();
        for (DictType dictType:dictTypeList) {
            TreeNode treeDictType = new TreeNode();
            treeDictType.setName(dictType.getDictName());
            treeDictType.setId(dictType.getId());
            treeDictType.setPId("0");
            treeDictType.setType(1);
            treeDictType.setIcon("/static/imgs/tree/cate.png");
            treeDictType.setOpen(false);
            treeList.add(treeDictType);
            List<DictData> dictDataList = dictDataService.getDictDataByType(dictType.getDictType());
            for (DictData dictData:dictDataList){
                TreeNode treedictData = new TreeNode();
                treedictData.setId(dictData.getId());
                treedictData.setType(2);
                treedictData.setPId(StringUtils.isBlank(dictData.getParentId())?dictType.getId():dictData.getParentId());
                treedictData.setName(dictData.getDictText());
                treedictData.setIcon("/static/imgs/tree/material.png");
                treeList.add(treedictData);
            }
        }
        String json = JSONArray.toJSONString(treeList);
        request.setAttribute("treesdDictData", json);
        return "dict/addDictData";
    }

    @RequestMapping("/insertDictData")
    @ResponseBody
    public ResponseMessage insertDictData(DictData dictData){
        return dictDataService.insertDictData(dictData);
    }

    /****
     * 新增字典类型/字典数据时，关闭子页签，重新加载树
     * @return
     */
    @RequestMapping("/treeReload")
    @ResponseBody
    public ResponseMessage treeReload(){
        List<TreeNode> treeList = new ArrayList<>();
        TreeNode rootTree = new TreeNode();
        rootTree.setId("0");
        rootTree.setPId("-1");
        rootTree.setIcon("/static/imgs/tree/company.png");
        rootTree.setType(0);
        rootTree.setName("全部字典");
        treeList.add(rootTree);
        List<DictType> dictTypeList = dictTypeService.getAllType();
        for (DictType dictType:dictTypeList) {
            TreeNode treeDictType = new TreeNode();
            treeDictType.setName(dictType.getDictName());
            treeDictType.setId(dictType.getId());
            treeDictType.setPId("0");
            treeDictType.setType(1);
            treeDictType.setIcon("/static/imgs/tree/cate.png");
            rootTree.setOpen(true);
            treeList.add(treeDictType);
            List<DictData> dictDataList = dictDataService.getDictDataByType(dictType.getDictType());
            for (DictData dictData:dictDataList){
                TreeNode treedictData = new TreeNode();
                treedictData.setId(dictData.getId());
                treedictData.setType(2);
                treedictData.setPId(StringUtils.isBlank(dictData.getParentId())?dictType.getId():dictData.getParentId());
                treedictData.setName(dictData.getDictText());
                treedictData.setIcon("/static/imgs/tree/material.png");
                treeList.add(treedictData);
            }
        }
        String json = JSONArray.toJSONString(treeList);
        return ResponseMessage.ok(json);
    }

    /**********
     * 根据选择树的ID查找信息
     * @param id
     * @return
     */
    @RequestMapping("/getCenterDatas")
    @ResponseBody
    public ResponseMessage getCenterDatas(String id){
        return ResponseMessage.ok(dictTypeService.getCenterDatas(id));
    }

    /*************
     * 单条删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteDcitData")
    @ResponseBody
    public ResponseMessage deleteDcitData(String id){
        return dictDataService.deleteDcitData(id);
    }

    /**********
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseMessage delBatch(String ids){
        return dictDataService.delBatch(ids);
    }

    /***********
     * 删除字典类型
     * @param id
     * @return
     */
    @RequestMapping("/deleteDictype")
    @ResponseBody
    public ResponseMessage deleteDictype(String id){
        return dictTypeService.deleteDictype(id);
    }
}
